package com.spvs.approval.service.impl;

import com.alibaba.csp.sentinel.util.StringUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spvs.approval.domain.ApprovalClassifyEntity;
import com.spvs.approval.domain.vo.ApprovalClassifyVo;
import com.spvs.approval.mapper.ApprovalClassifyMapper;
import com.spvs.approval.service.ApprovalClassifyService;
import com.spvs.approval.utils.GsonUtils;
import com.spvs.common.core.utils.StringUtils;
import com.spvs.common.core.web.domain.AjaxResult;
import com.spvs.common.security.utils.SecurityUtils;
import com.spvs.system.api.domain.SysRole;
import com.spvs.system.api.model.LoginUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Slf4j
@Service("approvalClassifyService")
@Transactional
public class ApprovalClassifyServiceImpl extends ServiceImpl<ApprovalClassifyMapper, ApprovalClassifyEntity> implements ApprovalClassifyService {

    @Autowired
    private ApprovalClassifyMapper approvalClassifyMapper;

    @Override
    public Page<ApprovalClassifyEntity> queryPage(Map<String, Object> params) {
        Page<ApprovalClassifyEntity> page = new Page<>(Long.parseLong((String) params.get("pageIndex")), Long.parseLong((String) params.get("pageSize")));
        params.remove("pageIndex");
        params.remove("pageSize");
        LoginUser loginUser = SecurityUtils.getLoginUser();
        List<SysRole> roles = loginUser.getSysUser().getRoles();
        String dataScope = "1";
        if (roles != null && roles.size() > 0) {
            SysRole sysRole = roles.get(0);
            dataScope = sysRole.getDataScope();
        }
        if (dataScope.equals("1")) {
            QueryWrapper<ApprovalClassifyEntity> wrapper = new QueryWrapper<>();
            wrapper.allEq(params).orderByDesc("id");
            return this.page(page, wrapper);
        }
        params.put("dept_id",loginUser.getSysUser().getDeptId());
        return approvalClassifyMapper.queryPage(page, params);
    }

    @Override
    public ApprovalClassifyEntity selectOneByName(String classifyName) {
        return approvalClassifyMapper.selectOneByName(classifyName);
    }

    @Override
    public ApprovalClassifyEntity selectOneByCode(String classifyCode) {
        return approvalClassifyMapper.selectOneByCode(classifyCode);
    }

    /**
     * 修改和保存 审批模版
     *
     * @param approvalClassifyVo
     * @return
     */
    @Override
    public AjaxResult saveOrModifyClassify(ApprovalClassifyVo approvalClassifyVo) throws Exception {
        // 转换模版对象，如果模版表面不存在那么生成模板编码 , 将节点集合转json保存到大字段中
        ApprovalClassifyEntity ac = new ApprovalClassifyEntity();
        if (null == approvalClassifyVo.getId()) {
            ac.setClassifyCode("SP" + IdWorker.getIdStr());
            ac.setCreateTime(new Date());
            ac.setType(approvalClassifyVo.getType());
        } else {
            ac = this.getById(approvalClassifyVo.getId());
            ac.setType(approvalClassifyVo.getType());
        }
        if (!StringUtils.isEmpty(approvalClassifyVo.getClassifyName())){
            ac.setClassifyName(approvalClassifyVo.getClassifyName());
        }
        if (!StringUtils.isEmpty(approvalClassifyVo.getRemark())){
            ac.setRemark(approvalClassifyVo.getRemark());
        }
        if (approvalClassifyVo.getJsonMap()!=null){
            ApprovalClassifyVo.JsonMap jsonMap = approvalClassifyVo.getJsonMap();
            String nodesJson = GsonUtils.ObjectToString(jsonMap);
            ac.setRuleShow(nodesJson);
        }
        if (!StringUtils.isEmpty(approvalClassifyVo.getCreateUser())){
            ac.setCreateUser(approvalClassifyVo.getCreateUser());
        }
        ac.setCreateUserId(approvalClassifyVo.getCreateUserId());
        ac.setDeptId(approvalClassifyVo.getDeptId());
        ac.setDeptName(approvalClassifyVo.getDeptName());

        boolean b = this.saveOrUpdate(ac);

        if (b) {
            return AjaxResult.success();
        }
        return AjaxResult.error();

    }

}
